package
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	import GuiEvent;

	import windows.CodeWindow;
	import windows.FormWindow;
	import windows.ComponentsWindow;
	import windows.propertyWindow.PropertiesWindow;
	import windows.treeWindow.TreeWindow;
	import windows.ActionsWindow;
	
	/**
	 * Попытка запилить свой UI редактор с блекджеком и шлюхами
	 * @author yaempechenki@gmail.com
	 */
	[SWF(frameRate="30",width="820",height="640",backgroundColor="#383838")]
	
	public class Main extends Sprite
	{
		
		private var components:ComponentsWindow;
		private var form:FormWindow;
		private var properties:PropertiesWindow;
		private var code:CodeWindow;
		private var tree:TreeWindow;
		private var actions:ActionsWindow;
		
		public function Main():void
		{
			if (stage) {
				init();
			} else {
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		
		private function init(event:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			components = new ComponentsWindow();
			components.addEventListener(MouseEvent.CLICK, onClick);
			addChild(components);
			
			form = new FormWindow();
			form.setFormSize(500, 400);
			form.addEventListener(MouseEvent.CLICK, onClick);
			addChild(form);
			
			properties = new PropertiesWindow();
			properties.addEventListener(MouseEvent.CLICK, onClick);
			properties.addEventListener(GuiEvent.ADD_SKETCH, onAddSketch);
			properties.addEventListener(GuiEvent.FORM_SIZE_CHANGED, onFormSizeChanged);
			addChild(properties);
			
			code = new CodeWindow();
			addChild(code);
			
			tree = new TreeWindow();
			tree.addEventListener(MouseEvent.CLICK, onClick);
			addChild(tree);
			
			actions = new ActionsWindow();
			actions.addEventListener(MouseEvent.CLICK, onClick);
			addChild(actions);
			
			stage.addEventListener(MouseEvent.CLICK, onTerraIncognitaClick);
			stage.addEventListener(Event.RESIZE, onResize);
			onResize(null);
		}
		
		private function onClick(event:MouseEvent):void
		{
			refresh();
		}
		
		private function onTerraIncognitaClick(event:MouseEvent):void
		{
			if (event.target as Stage) {
				Controller.instance.selectedObject = null;
				refresh();
			}
		}
		
		private function refresh():void
		{
			properties.refresh();
			code.refresh();
			form.refresh();
			tree.refresh();
			actions.refresh();
		}

        private function onAddSketch(event:GuiEvent):void {
            form.addSketch();
        }
		
		private function onFormSizeChanged(event:GuiEvent):void {
			form.setFormSize(event.data.width, event.data.height);
		}
		
		private function onResize(event:Event):void
		{
			const padding:int = 10;
			
			components.x = padding;
			components.y = padding;
			
			form.x = stage.stageWidth / 2 - form.width / 2;
			form.y = stage.stageHeight / 2 - form.height / 2;
			
			properties.x = padding;
			properties.y = components.height + padding * 2;
			
			code.x = stage.stageWidth - code.width - padding;
			code.y = stage.stageHeight - code.height - padding;
			
			tree.x = stage.stageWidth - tree.width - padding;
			tree.y = padding;
			
			actions.x = stage.stageWidth - actions.width - padding;
			actions.y = tree.y + tree.height + padding;
		}
	}
}

/**
 * Todo todo todo todo todo tototototodooo todo todo (мелодия из розовой пантеры)
 * 1-padding/margin
 * 2-к форме уметь добавлять скетч * устанавливать ему скейл
 * 3-прилипание к краям формы, когда чилда тащишь к краю - устанавливать ему алайн
 * 4-xml шаблон с текстами для создания элементов [constructor]
 * 5-кнопка, пора делать кнопку
 * имена объектам
 * устанавливать текстфилдовый текст
 */